package org.aceor.mddal.gateway.datatype.impl;

import com.google.common.math.LongMath;
import org.aceor.mddal.gateway.datatype.DataType;
import org.aceor.mddal.gateway.datatype.cal.AbstractCalculator;
import org.aceor.mddal.gateway.datatype.cal.Calculator;

import java.math.RoundingMode;

/**
 * Created by lxue on 16/4/16.
 */
public class LongType implements DataType<Long> {
    
    private Calculator calculator = new AbstractCalculator() {
        @Override
        public Object doAdd(Object value1, Object value2) {
            return LongMath.checkedAdd((Long)value1, (Long)value2);
        }

        @Override
        public Object doSub(Object value1, Object value2) {
            return LongMath.checkedSubtract((Long)value1, (Long)value2);
        }

        @Override
        public Object doMultiply(Object value1, Object value2) {
            return LongMath.checkedMultiply((Long)value1, (Long)value2);
        }

        @Override
        public Object doDivide(Object value1, Object value2) {
            return LongMath.divide((Long)value1, (Long)value2, RoundingMode.DOWN);
        }
    };
    
    @Override
    public int compare(Long o1, Long o2) {
        long result = LongMath.checkedSubtract(o1, o2);
        if (result > 0) {
            return 1;
        } else if (result < 0) {
            return -1;
        } else {
            return 0;
        }
    }

    @Override
    public Calculator getCalculator() {
        return calculator;
    }
}
